//
//  VectorClass2.cpp
//  Practice11
//
//  Created by 赵世杰 on 2020/7/25.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#include "VectorClass2.hpp"
#include <cmath>


using namespace std;


namespace VECTOR2 {


const double Red_to_deg = 45.0/atan(1.0);

//void Vector2::set_mag()
//{
//    mag = sqrt(x * x + y * y);
//}
//
//void Vector2::set_ang()
//{
//    if(x == 0 && y == 0)
//    {
//        ang = 0;
//    }
//    else
//    {
//        ang = atan2(y, x);
//    }
//}

double Vector2::magval() const
{
    return sqrt(x * x + y * y);
}
double Vector2::angval() const
{
    
    if(x == 0 && y == 0)
    {
        return 0;
    }
    else
    {
        return atan2(y, x);
    }
    
}

void Vector2::set_x()
{
    x = magval() * cos(angval());
}

void Vector2::set_y()
{
    y = magval() * sin(angval());
}

Vector2::Vector2()
{
    x = y = 0.0;
    
    mode = RECT;
}


Vector2::Vector2(double n1,double n2,Mode form)
{
    mode = form;
    if(form == RECT)
    {
        x = n1;
        y = n2;
//        set_mag();
//        set_ang();
    }
    else if (form == POL)
    {
//        mag = n1;
//        ang = n2/Red_to_deg;
        
//        set_x();
//        set_y();
        
        x = n1 * cos(n2/Red_to_deg);
        y = n1 * sin(n2/Red_to_deg);
        
    }
    else
    {
        cout << "Incorrect 3rd argument to Vector2() -- ";
        cout << "Vector2 set to 0\n";
        x = y = 0.0;
        mode = RECT;
        
    }
}

void Vector2::reset(double n1,double n2,Mode form)
{
    mode = form;
    
    if(form == RECT)
    {
        x = n1;
        y = n2;
//        set_mag();
//        set_ang();
    }
    else if(form == POL)
    {
//        mag = n1;
//        ang = n2/Red_to_deg;
//        set_x();
//        set_y();
        
        
        x = n1 * cos(n2/Red_to_deg);
        y = n1 * sin(n2/Red_to_deg);
        
    }
    else
    {
        cout << "Incorrect 3rd argument to Vector2() -- ";
        cout << "Vector2 set to 0\n";
        x = y = 0.0;
        mode = RECT;
    }
    
}

Vector2::~Vector2()
{
    
}

void Vector2::polar_mode()
{
    mode = POL;
}


void Vector2::rect_model()
{
    mode = RECT;
}



Vector2 Vector2::operator+(const Vector2 &b)const
{
    return Vector2(x + b.x , y+b.y);
}

Vector2 Vector2::operator-(const Vector2 &b)const
{
    return Vector2(x - b.x , y-b.y);
}

Vector2 Vector2::operator-()const
{
    return Vector2(-x,-y);
}


Vector2 Vector2::operator*(double n)const
{
    return Vector2(n*x,n*y);
}



Vector2 operator*(double n,const Vector2 &a)
{
    return a*n;
}
std::ostream & operator<<(std::ostream & os,const Vector2 & v)
{
    if(v.mode == Vector2::RECT)
    {
        os << "(x,y) = (" << v.x << "," << v.y << ")";
    }
    else if (v.mode == Vector2::POL)
    {
        os << "(m,a) = (" << v.magval() << "," << v.angval() * Red_to_deg << ")";
    }
    else
    {
        os << "Vector2 object mode is invalid";
    }
    
    return os;
}

}

